package com.cloudpark.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudpark.common.util.Constants;
import com.cloudpark.common.util.DateUtils;
import com.cloudpark.common.util.ObjectUtil;
import com.cloudpark.model.entity.CardOrder;
import com.cloudpark.model.entity.CardOrderTemp;
import com.cloudpark.model.entity.ParkOrder;
import com.cloudpark.model.entity.ParkOrderTemp;
import com.cloudpark.model.mapper.CardOrderMapper;
import com.cloudpark.model.mapper.CardOrderTempMapper;
import com.cloudpark.model.mapper.ParkOrderMapper;
import com.cloudpark.model.mapper.ParkOrderTempMapper;
import com.cloudpark.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private ParkOrderMapper parkOrderMapper;
	@Autowired
	private ParkOrderTempMapper parkOrderTempMapper;
	@Autowired
	private CardOrderMapper cardOrderMapper;
	@Autowired
	private CardOrderTempMapper cardOrderTempMapper;

	@Override
	public void saveParkOrder(ParkOrder order) {
		parkOrderMapper.insertSelective(order);
	}

	@Override
	public void saveParkOrderTemp(ParkOrderTemp orderTemp) {
		parkOrderTempMapper.insertSelective(orderTemp);
	}

	@Override
	public Long syncOrderFromTemp(String orderNo, String payTime) {
		if (orderNo.startsWith(Constants.ORDER_CATEGORY_TC)) {
			List<ParkOrderTemp> temps = parkOrderTempMapper.selectByOrderNo(orderNo);
			List<ParkOrder> formals = parkOrderMapper.selectByOrderNo(orderNo);
			ParkOrder order = null;
			if (ObjectUtil.isBlank(formals)) {
				order = new ParkOrder();
				ParkOrderTemp orderTemp = temps.get(0);
				order.copy(orderTemp);
				if (ObjectUtil.isNotBlank(payTime)) {
					order.setPayTime(DateUtils.toDate(payTime, DateUtils.YMDHMS2));
				}
				parkOrderMapper.insertSelective(order);
				return order.getId();
			} else {
				order = formals.get(0);
				if (ObjectUtil.isNotBlank(payTime)) {
					order.setPayTime(DateUtils.toDate(payTime, DateUtils.YMDHMS2));
				}
				parkOrderMapper.updateByPrimaryKey(order);
				return order.getId();
			}
		} else if (orderNo.startsWith(Constants.ORDER_CATEGORY_YK)) {
			List<CardOrderTemp> temps = cardOrderTempMapper.selectByOrderNo(orderNo);
			List<CardOrder> formals = cardOrderMapper.selectByOrderNo(orderNo);
			CardOrder cardOrder = null;
			if (ObjectUtil.isBlank(formals)) {
				cardOrder = new CardOrder();
				CardOrderTemp cardOrderTemp = temps.get(0);
				cardOrder.copy(cardOrderTemp);
				if (ObjectUtil.isNotBlank(payTime)) {
					cardOrder.setRechargeTime(DateUtils.toDate(payTime, DateUtils.YMDHMS2));
				}
				cardOrderMapper.insertSelective(cardOrder);
				return cardOrder.getId();
			} else {
				cardOrder = formals.get(0);
				if (ObjectUtil.isNotBlank(payTime)) {
					cardOrder.setRechargeTime(DateUtils.toDate(payTime, DateUtils.YMDHMS2));
				}
				cardOrderMapper.updateByPrimaryKey(cardOrder);
				return cardOrder.getId();
			}
		}
		return null;
	}

	@Override
	public ParkOrder queryParkOrderById(Long orderId) {
		return parkOrderMapper.selectByPrimaryKey(orderId);
	}
	
	@Override
	public ParkOrder queryParkOrderByOrderNo(String orderNo) {
		List<ParkOrder> list = parkOrderMapper.selectByOrderNo(orderNo);
		if (ObjectUtil.isNotBlank(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public ParkOrderTemp queryParkOrderTempByOrderNo(String orderNo) {
		List<ParkOrderTemp> list = parkOrderTempMapper.selectByOrderNo(orderNo);
		if (ObjectUtil.isNotBlank(list)) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public CardOrder queryCardOrderByOrderNo(String orderNo) {
		List<CardOrder> list = cardOrderMapper.selectByOrderNo(orderNo);
		if (ObjectUtil.isNotBlank(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public CardOrderTemp queryCardOrderTempByOrderNo(String orderNo) {
		List<CardOrderTemp> list = cardOrderTempMapper.selectByOrderNo(orderNo);
		if (ObjectUtil.isNotBlank(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public void saveCardOrderTemp(CardOrderTemp cardOrderTemp) {
		cardOrderTempMapper.insertSelective(cardOrderTemp);
	}

	@Override
	public CardOrder queryCardOrderById(Long orderId) {
		return cardOrderMapper.selectByPrimaryKey(orderId);
	}

}
